package com.care.service.impl;

import com.care.dao.mapper.SysLogMapper;
import com.care.dao.model.Admin;
import com.care.dao.model.Service;
import com.care.dao.model.SysLog;
import com.care.dao.model.User;
import com.care.service.ServiceInterface;
import com.care.service.SysLogService;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.Date;

@org.springframework.stereotype.Service
public class SysLogServiceImpl implements SysLogService {

    @Autowired
    private SysLogMapper sysLogMapper;

    @Override
    public void userLoginLog(User user, boolean isSuccess) {
        SysLog log = new SysLog();
        log.setLogType(1);
        log.setLogTime(new Date());
        log.setLogName("小程序用户登录");
        log.setOperatorName(user.getNickName());
        if(user.getStatus()==-1){
            isSuccess=false;
        }
        if(isSuccess){
            log.setOperatResult("登录成功");
        }else{
            log.setOperatResult("登录失败");
        }
        sysLogMapper.insertSelective(log);
    }

    @Override
    public void userLogoutLog(User user) {
        SysLog log = new SysLog();
        log.setLogType(1);
        log.setLogTime(new Date());
        log.setLogName("小程序用户退出登录");
        log.setOperatorName(user.getNickName());
        log.setOperatResult("登出成功");
        sysLogMapper.insertSelective(log);
    }

    @Override
    public void adminLoginLog(Admin admin, boolean isSuccess) {
        SysLog log = new SysLog();
        log.setLogType(1);
        log.setLogTime(new Date());
        if(admin.getRole()==1){
            log.setLogName("系统管理员登录");
        }else{
            log.setLogName("社区理员登录");
        }
        log.setOperatorName(admin.getRealName());
        if(isSuccess){
            log.setOperatResult("登录成功");
        }else{
            log.setOperatResult("登录失败");
        }
        sysLogMapper.insertSelective(log);
    }

    @Override
    public void adminLogoutLog(Admin admin) {
        SysLog log = new SysLog();
        log.setLogType(1);
        log.setLogTime(new Date());
        if(admin.getRole()==1){
            log.setLogName("系统管理员退出登录");
        }else{
            log.setLogName("社区理员退出登录");
        }
        log.setOperatorName(admin.getRealName());
        log.setOperatResult("登出成功");
        sysLogMapper.insertSelective(log);
    }

    @Override
    public void publishServiceLog(Service service, User user) {
        SysLog log = new SysLog();
        log.setLogType(2);
        log.setLogTime(new Date());
        log.setLogName("社区老人发布服务："+service.getTitle());
        log.setOperatorName(user.getNickName());
        log.setOperatResult("发布成功");
        sysLogMapper.insertSelective(log);
    }

    @Override
    public void updateServiceLog(Service service, Integer newStatus, User user) {
        SysLog log = new SysLog();
        log.setLogType(2);
        log.setLogTime(new Date());
        if(newStatus == ServiceInterface.SERVICE_STATUS_PROCESSING){
            log.setLogName("志愿者接单《"+service.getTitle()+"》服务");
        }else if(newStatus == ServiceInterface.SERVICE_STATUS_FINISH){
            log.setLogName("《"+service.getTitle()+"》服务完成");
        }else if(newStatus == ServiceInterface.SERVICE_STATUS_FEEDBACK){
            log.setLogName("服务反馈");
        }else if(newStatus == ServiceInterface.SERVICE_STATUS_CANCEL){
            log.setLogName("服务取消");
        }
        log.setOperatorName(user.getNickName());
        log.setOperatResult("操作成功");
        sysLogMapper.insertSelective(log);
    }
}
